﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.Catalog
{
    public partial class FrmKhoahoc : PRE.Catalog.FrmBase
    {
        DAL.EntitesDAL.KhoahocDAL _dal = new DAL.EntitesDAL.KhoahocDAL();
        BLL.EntitiesBLL.KhoahocBLL _bll = new BLL.EntitiesBLL.KhoahocBLL();

        public FrmKhoahoc()
        {
            InitializeComponent();
        }


        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            DataTable _ma = _dal.Select_Max();
            if (_ma != null)
                if (_ma.Rows.Count > 0)
                {
                    string ma = _bll.AddID(_ma.Rows[0]["Ma"] + "");
                    txtMakhoa.Text = ma;

                    ReadOnlyControl(false);
                    ClearDataBindings();
                    ChangeStatus(false);
                    ResetText();

                    txtTenkhoa.Focus();
                }

            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            var _id = grvMain.GetFocusedRowCellValue("Makhoa") + "";
            
            DialogResult result;
            var ok = _dal.Delete(_id);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            txtTenkhoa.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
         //   btnGoSearch.Visible = false;
            txtMakhoa.Properties.ReadOnly = true;

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }



            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            if (Check())
            {
                var name = Proper(txtTenkhoa.Text);
                if (IsAdd)
                {
                    var o = new DAL.Entities.Khoahoc()
                    {
                        Makhoa = txtMakhoa.Text,
                        Tenkhoa = name,
                        Ngaykhaigiang = Convert.ToDateTime(dteNgaykhaigiang.EditValue),
                        Ngayketthuc = Convert.ToDateTime(dteNgaykt.EditValue)
                    };

                    var oki = _dal.Insert(o);
                    if (oki)
                    {
                        //MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        PerformRefresh();
                        ChangeStatus(false);
                        PerformAdd();
                    }
                    else
                    {
                        MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        ChangeStatus(false);
                        ReadOnlyControl(false);
                    }

                }

                if (IsEdit)
                {
                    var _id = grvMain.GetFocusedRowCellValue("Makhoa") + "";

                    var o = new DAL.Entities.Khoahoc()
                    {
                        Makhoa = Convert.ToString(_id),
                        Tenkhoa = name,
                        Ngaykhaigiang = Convert.ToDateTime(dteNgaykhaigiang.EditValue),
                        Ngayketthuc = Convert.ToDateTime(dteNgaykt.EditValue)
                    };

                    var oki = _dal.Update(o);
                    if (oki)
                    {
                        // MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        PerformRefresh();
                        ChangeStatus();
                    }
                    else
                    {
                        MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        ChangeStatus(false);
                        ReadOnlyControl(false);
                    }
                }

            }
            else
            {
                MessageBox.Show("Ngày kết thúc phải lớn hơn ngày bắt đầu ...", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dteNgaykt.Focus();
            }
            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            txtTenkhoa.Text = null;
            dteNgaykhaigiang.EditValue = DateTime.Now;
            dteNgaykt.EditValue = DateTime.Now;

            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtMakhoa.DataBindings.Clear();
            txtTenkhoa.DataBindings.Clear();
            dteNgaykhaigiang.DataBindings.Clear();
            dteNgaykt.DataBindings.Clear();

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtMakhoa.DataBindings.Add("EditValue", _dtb, ".Makhoa");
            txtTenkhoa.DataBindings.Add("EditValue", _dtb, ".Tenkhoa");
            dteNgaykhaigiang.DataBindings.Add("EditValue", _dtb, ".Ngaykhaigiang");
            dteNgaykt.DataBindings.Add("EditValue", _dtb, ".Ngayketthuc");

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            //txtMaHV.Properties.ReadOnly = isReadOnly;
            txtTenkhoa.Properties.ReadOnly = isReadOnly;
            dteNgaykhaigiang.Properties.ReadOnly = isReadOnly;
            dteNgaykt.Properties.ReadOnly = isReadOnly;

            grcMain.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            ClearDataBindings();
            ResetText();
            ChangeStatus(false);
         //   btnGoSearch.Visible = true;

            dteNgaykhaigiang.Properties.ReadOnly = true;
            dteNgaykt.Properties.ReadOnly = true;

            txtTenkhoa.Properties.ReadOnly = false;
            txtMakhoa.Properties.ReadOnly = false;
            txtMakhoa.ResetText();
            txtMakhoa.Focus();
            grcMain.Enabled = true;

            base.PerformSearch();
        }

        /// <summary>
        /// Execute Search data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void btnGoSearch_Click(object sender, EventArgs e)
        //{
        //    string name = Proper(txtTenkhoa.Text);

        //    var o = new DAL.Entities.Khoahoc()
        //    {
        //        Makhoa = txtMakhoa.Text,
        //        Tenkhoa = name
        //    };

        //    grcMain.DataSource = _dal.Search(o);
        //}

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            _dtb = _dal.Select();

            //_dtb.WriteXml("C:\\a.xml");

            //_dtb.ReadXml("C:\\a.xml");

            if (_dtb != null)
            {
                grcMain.DataSource = _dtb;
                //gridColumn2.BestFit(); // fit column                
            }

            base.LoadData();
        }
        #endregion

        private void FrmKhoahoc_Load(object sender, EventArgs e)
        {
            DataTable dt = _dal.Select_Current(DateTime.Now);    
            PerformRefresh();

        }

        private bool Check()
        {
            DateTime _ngaybd = Convert.ToDateTime(dteNgaykhaigiang.EditValue);
            DateTime _ngaykt = Convert.ToDateTime(dteNgaykt.EditValue);

            if (_ngaykt <= _ngaybd)
                return false;
            return true;
        }

    }
}
